﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class EditEvent : System.Web.UI.Page
    {
        AppLib.Event EventToEdit;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            EventToEdit = MidApp.ManageEvent.GetEventByID(int.Parse(Server.HtmlEncode(Request.Cookies["EventIDtoEdit"].Value)));
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            AssignTodaysDate();

            TextBoxEventName.Text = EventToEdit.EventName;
            //TextBoxPrice.Text = EventToEdit.Price.ToString();
            TextBoxSeats.Text = EventToEdit.Seats.ToString();
            TextBoxDescription.Text = EventToEdit.Description;
            TextBoxAdress.Text = EventToEdit.Address;
            TextBoxDate.Text = EventToEdit.DateOfHolding.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (int.Parse(TextBoxSeats.Text) < MidApp.ManageParticipate.GetParticipantsOnEvent(EventToEdit.EventID).Count)
            {
                LabelSeatsMissCount.Visible = true;
                return;
            }
            EventToEdit.EventName = TextBoxEventName.Text;
            EventToEdit.Description = TextBoxDescription.Text;
            //EventToEdit.Price = int.Parse(TextBoxPrice.Text);
            EventToEdit.Address = TextBoxAdress.Text;
            EventToEdit.Seats = int.Parse(TextBoxSeats.Text);

            //kontrola zda datum neni v minulosti
            try
            {
                if (DateTime.Compare(DateTime.Now, DateTime.Parse(TextBoxDate.Text)) > 0)
                {
                    Label1.Text = "Nelze vypsat akci v minulosti";
                    Label1.Visible = true;
                    return;
                }
                else
                {
                    EventToEdit.DateOfHolding = DateTime.Parse(TextBoxDate.Text);
                }
            }
            catch (Exception)
            {
                Label1.Text = "Špatný formát data";
                Label1.Visible = true;
                return;
            }

            MidApp.ManageEvent.AssignNewEventInfo(EventToEdit);
            Label1.Text = "Změna údaju proběhla úspěšně";
            Label1.Visible = true;
            Response.Redirect("EventsList.aspx");
        }

        public void AssignTodaysDate()
        {
            CalendarExtender1.StartDate = DateTime.Today;
        }
    }
}